import { NextResponse } from 'next/server'
import { verifyJWT } from '@/lib/jwt'
import { cookies } from 'next/headers'

export async function GET() {
  try {
    const cookieStore = await cookies()
    const token = cookieStore.get('vertu-auth-token')?.value

    if (!token) {
      return NextResponse.json({ 
        success: false,
        message: '未找到认证令牌' 
      }, { status: 401 })
    }

    const payload = await verifyJWT(token)

    if (!payload) {
      return NextResponse.json({ 
        success: false,
        message: '认证令牌无效或已过期' 
      }, { status: 401 })
    }

    return NextResponse.json({ 
      success: true,
      message: '认证有效',
      data: {
        user: {
          id: payload.userId,
          username: payload.username
        }
      }
    }, { status: 200 })

  } catch (error: unknown) {
    console.error('Token 验证失败:', error)
    let errorMessage = '内部服务器错误'
    if (error instanceof Error) {
      errorMessage = error.message
    }
    return NextResponse.json({ 
      success: false,
      message: errorMessage 
    }, { status: 500 })
  }
}